package cn.itcast.consumption.Dao;

import cn.itcast.consumption.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class UserDao {

   /* @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient; // eureka客户端，可以获取到eureka中服务的信息

    @HystrixCommand(fallbackMethod = "findUserFallback")
    public User findUser(Long id) {
        List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        // 因为只有一个UserService。所以获取第一个实例
        ServiceInstance instance = instances.get(0);
        // 获取ip和端口信息，拼接成服务地址
        // String baseUrl = "http://user-service/user/user/" + id;
        // return this.restTemplate.getForObject(baseUrl, User.class);
    }

    public User findUserFallback(Long id){

        User user = new User();
        user.setUserName("查询错误");
        return user;

    }*/
}
